package com.ruoyi.wechat.service.impl;

import com.ruoyi.common.utils.ConvertUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.wechat.domain.Message;
import com.ruoyi.wechat.domain.ScenicSpots;
import com.ruoyi.wechat.domain.WxScenicSpots;
import com.ruoyi.wechat.mapper.ScenicSpotsMapper;
import com.ruoyi.wechat.service.IMessageService;
import com.ruoyi.wechat.service.IWxScenicSpotsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>ClassName : WxScenicSpotsServiceImpl</p>
 * <p>Description : </p>
 *
 * @author 孙伟光
 * @version 1.0
 * @date 2024/7/29 8:41
 */
@Service
public class WxScenicSpotsServiceImpl implements IWxScenicSpotsService {

    @Autowired
    private ScenicSpotsMapper scenicSpotsMapper;

    @Autowired
    private IMessageService messageService;

    /**
     * 根据营地Id查询该营地的所有周边景点
     * @param campId 营地Id
     * @return 该营地的周边景点列表
     */
    @Override
    public List<WxScenicSpots> getInfoByCampId(String campId) {
        //封装条件
        ScenicSpots scenicSpots = new ScenicSpots();
        scenicSpots.setCampId(campId);
        scenicSpots.setApprove("1");
        //根据营地Id获取营地下的周边景点列表
        List<ScenicSpots> list = scenicSpotsMapper.selectScenicSpotsList(scenicSpots);
        //数据类型转换
        if (list != null && list.size() > 0) {
            return ConvertUtils.sourceToTarget(list, WxScenicSpots.class);
        }
        return null;
    }

    /**
     * 根据周边景点Id获取详细信息
     * @param scenicId 周边景点Id
     * @return 周边景点对象
     */
    @Override
    public WxScenicSpots getInfo(String scenicId) {
        //根据周边景点id查询对象
        ScenicSpots scenicSpots = scenicSpotsMapper.selectScenicSpotsByScenicId(scenicId);
        //转换类型并返回
        return ConvertUtils.sourceToTarget(scenicSpots, WxScenicSpots.class);
    }

    /**
     * 新增周边景点
     *
     * @param wxScenicSpots 周边景点
     * @return 结果
     */
    @Override
    public int insertScenicSpots(WxScenicSpots wxScenicSpots) {
        //设置条件
        wxScenicSpots.setScenicId(IdUtils.randomUUID());
        wxScenicSpots.setApprove("0");    //默认为未审核
        //数据类型转换
        ScenicSpots scenicSpots = ConvertUtils.sourceToTarget(wxScenicSpots, ScenicSpots.class);
        return scenicSpotsMapper.insertScenicSpots(scenicSpots);
    }

    /**
     * 修改周边景点
     *
     * @param wxScenicSpots 周边景点
     * @return 结果
     */
    @Override
    public int updateScenicSpots(WxScenicSpots wxScenicSpots) {
        //设置条件
        wxScenicSpots.setApprove("0");    //默认为未审核
        //数据类型转换
        ScenicSpots scenicSpots = ConvertUtils.sourceToTarget(wxScenicSpots, ScenicSpots.class);
        return scenicSpotsMapper.updateScenicSpots(scenicSpots);
    }

    /**
     * 删除周边景点信息
     *
     * @param scenicId 周边景点主键
     * @return 结果
     */
    @Override
    public int deleteScenicSpotsByScenicId(String scenicId) {
        return scenicSpotsMapper.deleteScenicSpotsByScenicId(scenicId);
    }

    /**
     * 审核周边景点并发送消息
     * @param scenicId 周边景点Id
     * @param approve 审核状态
     * @param content 审核内容
     * @return 结果
     */
    @Override
    @Transactional
    public int approve(String scenicId, String approve, String content) {
        //营地周边景点条件对象
        ScenicSpots scenicSpots = new ScenicSpots();
        scenicSpots.setScenicId(scenicId);
        scenicSpots.setApprove(approve);
        //更新审核条件
        int rows = scenicSpotsMapper.updateScenicSpots(scenicSpots);
        //根据周边景点Id查询对象
        ScenicSpots scenicSpots1 = scenicSpotsMapper.selectScenicSpotsByScenicId(scenicId);
        //封装消息对象
        Message message = new Message();
        message.setCampId(scenicSpots1.getCampId());
        message.setContent(content);
        //调用消息
        int row = messageService.insertMessageByApprove(message,"2", approve, scenicSpots1.getScenicName());

        return rows;
    }


}
